package com.chub.study.util.jsr310;


import cn.hutool.core.date.DatePattern;
import com.chub.study.util.JsonDateDeserializer;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author : chub
 * @version : V1.0.0
 * @fileName : Person.java
 * @description : TODO
 * @createTime : 2021-11-21 0:48
 */

@Data
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Person {

    private String age;
    private String name;

//    @JsonFormat(pattern = "yyyy-MM-dd")  不能只转成日期，否则转回来报错，  LocalDateTime 不能直接从"yyyy-MM-dd"格式字符串转回
//    @JsonFormat 这个注解用来解析JSON数据中的日期字符串，会序列化返回数据

//    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATETIME_PATTERN)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime birthDay;
//    Java对象-->JSON格式对象: {"age":"45","name":"张三","map":null,"birthDay":"1987-04-05 12:50:12"}
//    JSON格式对象-->Java对象: Person(age=45, name=张三, map=null, birthDay=1987-04-05T12:50:12)

    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime birthDay1;
//    Java对象-->JSON格式对象: {"age":"45","name":"张三","birthDay1":[1988,4,5,12,50,12]}
//    JSON格式对象-->Java对象: Person(age=45, name=张三,  birthDay1=1988-04-05T12:50:12)

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATE_PATTERN)
    @JsonSerialize(using = LocalDateSerializer.class)   //序列化：将JAVA对象中的LocalDate转成JSON格式的日期字符串
    @JsonDeserialize(using = LocalDateDeserializer.class) //反序列化：将JSON格式的日期字符串转成JAVA对象中的LocalDate
    private LocalDate birthDay2;
//    Java对象-->JSON格式对象: {"age":"45","name":"张三","birthDay2":"1989-04-05"}
//    JSON格式对象-->Java对象: Person(age=45, name=张三, birthDay2=1989-04-05)

//    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
//    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
    private java.sql.Date birthDay3;
    //不加@JsonFormat注解，则如下
//    Java对象-->JSON格式对象: {"age":"45","name":"张三",..."birthDay3":1640828385039}
//    JSON格式对象-->Java对象:  Person(age=45, name=张三,... birthDay3=2021-12-30)
    //加了@JsonFormat注解，则如下
//    Java对象-->JSON格式对象: {"age":"45","name":"张三",..."birthDay3":"2021-12-30"}
//    JSON格式对象-->Java对象:  Person(age=45, name=张三,... birthDay3=2021-12-30)

//    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
    private java.util.Date birthDay4;
    //不加@JsonFormat注解，则如下
//    Java对象-->JSON格式对象: {"age":"45","name":"张三","birthDay4":1640828385039}
//    JSON格式对象-->Java对象:  Person(age=45, name=张三, birthDay4=Thu Dec 30 09:34:35 CST 2021)
    //加了@JsonFormat注解，则如下
//    Java对象-->JSON格式对象: {"age":"45","name":"张三", "birthDay4":"2021-12-30"}
//    JSON格式对象-->Java对象:  Person(age=45, name=张三,  birthDay4=Thu Dec 30 00:00:00 CST 2021)


//    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATE_PATTERN, timezone = "GMT+8")
    private java.sql.Timestamp birthDay5;
//不加@JsonFormat注解，则如下
//    Java对象-->JSON格式对象: {"age":"45","name":"张三","birthDay5":1640830858620}
//    JSON格式对象-->Java对象:  Person(age=45, name=张三, birthDay5=2021-12-30 10:19:08.789)
//加了@JsonFormat注解，则如下
//    Java对象-->JSON格式对象: {"age":"45","name":"张三","birthDay5":"2021-12-30 10:20:06"}
//    JSON格式对象-->Java对象:  Person(age=45, name=张三, birthDay5=2021-12-30 10:19:08.789)

//    java.sql.Date、 java.sql.Timestamp、 java.util.Date 都支持将long型日期反序列化
//    java.sql.Date、 java.sql.Timestamp、 java.util.Date 都支持将"yyyy-MM-dd"型日期反序列化 设置pattern = DatePattern.NORM_DATE_PATTERN即可
//    java.sql.Timestamp 支持将"yyyy-MM-dd hh:mm:ss"型时间反序列化


//    @DateTimeFormat(pattern = "yyyy-MM", fallbackPatterns = "")
//    @DateTimeFormat(pattern = "yyyy-MM-dd")
//    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_MONTH_PATTERN, timezone = "GMT+8")
    private java.util.Date birthDay6;


    //@JsonFormat在序列化和反序列化时都会用到
//    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
    @JsonSerialize(using = LocalDateTimeSerializer.class)

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
//    @DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
    @JsonDeserialize(using = JsonDateDeserializer.class)
    private LocalDateTime birthDay7;
}
